/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.loginRegister;

import java.io.IOException;
import java.sql.*;
import javax.servlet.ServletException;
import javax.servlet.http.*;
import javax.swing.JOptionPane;
/**
 *
 * @author Administrator
 */
public class RegisterServlet extends HttpServlet {

    /**
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code>
     * methods.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    public void wrong1(){    //对话框提示信息
        String msg="不允许有空值，注册失败！";
        int type=JOptionPane.YES_NO_CANCEL_OPTION;
        String title="提示信息";
        JOptionPane.showMessageDialog(null,msg,title,type);
    }
    public void wrong2(){    //对话框提示信息
        String msg="两次密码不同，注册失败！";
        int type=JOptionPane.YES_NO_CANCEL_OPTION;
        String title="提示信息";
        JOptionPane.showMessageDialog(null,msg,title,type);
    }
    public void wrong3(){    //对话框提示信息
        String msg="用户名已存在，注册失败！";
        int type=JOptionPane.YES_NO_CANCEL_OPTION;
        String title="提示信息";
        JOptionPane.showMessageDialog(null,msg,title,type);
    }
    public void right(){    //对话框提示信息
        String msg="注册信息合格，注册成功！";
        int type=JOptionPane.YES_NO_CANCEL_OPTION;
        String title="提示信息";
        JOptionPane.showMessageDialog(null,msg,title,type);
    }
    protected void doGet(HttpServletRequest request,HttpServletResponse response)throws ServletException,IOException{
        String userNumber=new String(request.getParameter("usernumber").getBytes("ISO-8859-1"),"UTF-8");
        String userName=new String(request.getParameter("username1").getBytes("ISO-8859-1"),"UTF-8");
        String password1=new String(request.getParameter("password1").getBytes("ISO-8859-1"),"UTF-8");
        String password2=new String(request.getParameter("password2").getBytes("ISO-8859-1"),"UTF-8");
        if(userNumber.length()==0||userName.length()==0||password1.length()==0||password2.length()==0){
            wrong1();
            response.sendRedirect("http://localhost:8084/jspsxxxx/register/register/zhuce.html");
        }else if(!(password1.equals(password2))){
            wrong2();
            response.sendRedirect("http://localhost:8084/jspsxxxx/register/register/zhuce.html");
        }
        else{
            try{
                Connection con=null;
                Statement stmt=null;
                ResultSet rs=null;
                Class.forName("com.mysql.jdbc.Driver");
                String url="jdbc:mysql://localhost:3306/abin? useUnicode=true&characterEncoding=gbk";
                con=DriverManager.getConnection(url,"root","admin");
                stmt=con.createStatement();
                String sql1="select * from t_user where usernumber='"+userNumber+"'";
                rs=stmt.executeQuery(sql1);
                rs.last();
                int k;
                k=rs.getRow();
                if(k>0){
                    wrong3();
                    response.sendRedirect("http://localhost:8084/jspsxxxx/register/register/zhuce.html");
                }else{
                    String sql2="insert into t_user values("+"'"+userName+"'"+","+"'"+userNumber+"'"+","+"'"+password1+"'"+")";
                    stmt.executeUpdate(sql2);
                }
                rs.close();
                stmt.close();
                con.close();
                right();
                response.sendRedirect("http://localhost:8084/jspsxxxx/login/login/denglu.html");
            }catch(Exception e){
                e.printStackTrace();
            }
        }
    }
      protected void doPost(HttpServletRequest request,HttpServletResponse response)
            throws ServletException,IOException{
        doGet(request,response);
    }
}
